﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;

namespace DuBaiCoffee2.Model
{
    public class IOMsgR
    {
        private static IOMsgR instance;
        private IOMsgR() { }

        public static IOMsgR Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsgR();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        string nostrs = "";
        public Thread All_thr;
        public bool isRobotBol = false;
        /// <summary>
        /// 程序运行点位关闭
        /// </summary>
        public void QK()
        {
            try
            {
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotKFJR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotJBR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotLHR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotCBWCR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotDBR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotJNWCR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotXBR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotBPJNR_Down();
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 机器人功能按钮关闭
        /// 状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        public void RobotQK(double type = 12)
        {
            try
            {
                if (type == 12 || type == 11)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRTZYXR_Down();
                }
                if (type == 12 || type == 7)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRYXR_Down();
                }
                if (type == 12 || type == 9)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRBJQRR_Down();
                }
                if (type == 12 || type == 3)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRSDR_Down();
                }
                if (type == 12 || type == 4)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRXDR_Down();
                }
                if (type == 12 || type == 5)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRSNR_Down();
                }
                if (type == 12 || type == 6)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRXSNR_Down();
                }
                if (type == 12 || type == 10)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRZTR_Down();
                }
                if (type == 12 || type == 8)
                {
                    Thread.Sleep(100);
                    RobotModelRight.Instance.RobotJQRJXYXR_Down();
                }
                Thread.Sleep(100);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IOSerial_Thr()
        {
            try
            {
                bool isRobot = false;
                RobotSerialPort.Instance.Initialization(IPorPortInfo.Instance.RobotIPRight, IPorPortInfo.Instance.RobotPortRight);
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotJQRSDR_UP();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotJQRSNR_UP();
                Thread.Sleep(1000);
                RobotModelRight.Instance.RobotJQRSDR_Down();
                Thread.Sleep(100);
                RobotModelRight.Instance.RobotJQRSNR_Down();
                Thread.Sleep(100);
                while (!isRobot)
                {
                    isRobot = RobotModelRight.Instance.RRobotJQRYSN_Sel();
                    Thread.Sleep(100);
                }
                Instance.QK();
                Instance.RobotQK();
                isRobotBol = true;
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] result = new byte[IOSerialPortR.Instance.serialPort.BytesToRead];
                IOSerialPortR.Instance.serialPort.Read(result, 0, IOSerialPortR.Instance.serialPort.BytesToRead);
                string str = "";// $"{DateTime.Now}：\n";
                str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void DigitalMsgBol2(string str)
        {
            #region 右臂不用
            //try
            //{

            //    var Bol = str.Split(' ');
            //    bool bol = false;
            //    bool isEmail = false;
            //    if (WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol != (Bol[4] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol = Bol[4] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol != (Bol[10] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol = Bol[10] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol != (Bol[6] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol = Bol[6] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol != (Bol[8] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol = Bol[8] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol != (Bol[12] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol = Bol[12] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.LBQFK1_Bol != (Bol[14] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.LBQFK1_Bol = Bol[14] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.LBQFK2_Bol != (Bol[16] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.LBQFK2_Bol = Bol[16] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol != (Bol[22] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol = Bol[22] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol != (Bol[24] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol = Bol[24] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol != (Bol[18] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.Milk_Sel_Bol = Bol[18] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.Water_Sel_Bol != (Bol[20] == "01" ? true : false))
            //    {
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.Water_Sel_Bol = Bol[20] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol != (Bol[26] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.GT1_Sel_Bol = Bol[26] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol != (Bol[28] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.GT2_Sel_Bol = Bol[28] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol != (Bol[30] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol)
            //            isEmail = true;
            //        bol = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.GT3_Sel_Bol = Bol[30] == "01" ? true : false;
            //    if (WuLiaoCoffeeSel.Instance.YBJ_Water != (Bol[32] == "01" ? true : false))
            //    {
            //        if (WuLiaoCoffeeSel.Instance.YBJ_Water)
            //            isEmail = true;
            //    }
            //    WuLiaoCoffeeSel.Instance.YBJ_Water = Bol[32] == "01" ? true : false;
            //    //if (isEmail)
            //    //{
            //    //    TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
            //    //    testDelegate.BeginInvoke(null, null);
            //    //}
            //    if (bol)
            //    {
            //        ClientList clientList = new ClientList();
            //        clientList.MsgBol = WuLiaoCoffeeSel.Instance;
            //        clientList.code = 2;
            //        SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            //    }
            //}
            //catch (Exception)
            //{
            //    return;
            //}
            #endregion
        }
        public void MsgShow(string str)
        {
            try
            {
                int count = str.Length / 315;
                if (count > 0)
                {
                    var str1 = str.Substring(str.Length - count * 315, 315);
                    //Console.WriteLine(str1);
                    if (str1.Split(' ').Count() == 105)
                    {
                        DigitalMsgBol2(str1);
                        if (count > 1)
                            MsgShow(str.Substring(str.Length - (count - 1) * 315, (count - 1) * 315));
                    }
                    else
                    {
                        var nostr = str1.Split(' ');
                        int cc = int.TryParse(nostr[0].Substring(nostr[0].Length - 2, 2), out int a) ? a : -1;
                        if (cc != -1)
                        {
                            nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                            for (int i = 1; i < nostr.Count() - 1; i++)
                            {
                                nostrs += nostr[i] + " ";
                            }
                            if (nostrs.Substring(0, 5) == "02 04")
                            {
                                if (nostrs.Count() >= 315)
                                {
                                    DigitalMsgBol2(nostrs);
                                    nostrs = "";
                                }
                            }
                            else
                            {
                                nostrs = "";
                            }
                        }
                    }
                }
                else
                {

                    var nostr = str.Split(' ');
                    nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                    for (int i = 1; i < nostr.Count() - 1; i++)
                    {
                        nostrs += nostr[i] + " ";
                    }
                    if (nostrs.Substring(0, 5) == "02 04")
                    {
                        if (nostrs.Count() >= 315)
                        {
                            DigitalMsgBol2(nostrs);
                            nostrs = "";
                        }
                    }
                    else
                    {
                        if (nostrs.Substring(0, 5) == "04 02")
                        {
                            Console.WriteLine(nostrs);
                            var robstr = nostrs.Split(' ');
                            RobotBol = robstr[3] == "01" ? true : false;
                            Console.WriteLine(robstr[3] == "01" ? true : false);
                        }
                        nostrs = "";
                    }
                }

            }
            catch (Exception)
            {
                return;
            }
        }
        public class EmailShow
        {
            public string title { get; set; } = "Shortage warning";
            public int deviceId { get; set; } = IPorPortInfo.Instance.deviceID;
            public string content { get; set; } = "";
            public string fromname { get; set; } = "Coffee Shop";
        }
        #endregion
    }
}
